package com.wanmait.recruit.controller;

import com.github.pagehelper.PageInfo;
import com.wanmait.recruit.pojo.Person;
import com.wanmait.recruit.pojo.PersonInfo;
import com.wanmait.recruit.pojo.PersonSystemInfo;
import com.wanmait.recruit.service.PersonInfoService;
import com.wanmait.recruit.service.PersonSystemInfoService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

@Controller
@RequestMapping("/web/news")
public class PersonNewsController {

    @Resource
    PersonSystemInfoService personSystemInfoService;

    @Resource
    PersonInfoService personInfoService;


    @RequestMapping("/systemNewsData")
    public ModelAndView systemNewsData(HttpSession session){
        ModelAndView mv = new ModelAndView();
        mv.setViewName("web/news/systemNewsData");
        Person person = (Person)session.getAttribute("person");
        PageInfo<PersonSystemInfo> pageInfo = personSystemInfoService.findAll(person);
        mv.addObject(pageInfo);
        mv.addObject(pageInfo.getList());
        return mv;
    }
    @RequestMapping("/systemNews")
    public ModelAndView systemNews(HttpSession session){
        ModelAndView mv = new ModelAndView();
        mv.setViewName("web/news/systemNews");
        Person person = (Person)session.getAttribute("person");
        PageInfo<PersonSystemInfo> pageInfo = personSystemInfoService.findAll(person);
        mv.addObject(pageInfo);
        mv.addObject(pageInfo.getList());
        return mv;
    }
    //查看系统消息
    @RequestMapping("/systemNews/read")
    public ModelAndView systemNewsRead(Integer id){
        ModelAndView mv = new ModelAndView();
        mv.setViewName("/web/news/systemNewsInfo");
        PersonSystemInfo personSystemInfo = personSystemInfoService.findById(id);
        mv.addObject(personSystemInfo);
        //设为已读
        personSystemInfo.setIsRead(true);
        personSystemInfoService.update(personSystemInfo);
        return mv;
    }
    //删除系统消息
    @RequestMapping("/systemNews/del")
    public void systemNewsDel(Integer id,HttpServletResponse response) throws IOException {
        PersonSystemInfo personSystemInfo = new PersonSystemInfo();
        personSystemInfo.setId(id);
        personSystemInfo.setEnable(false);
        personSystemInfoService.update(personSystemInfo);
        PrintWriter out = response.getWriter();
        out.print(1);
    }
    //查看个人消息
    @RequestMapping("/personNews/read")
    public ModelAndView personNewsRead(Integer id){
        ModelAndView mv = new ModelAndView();
        mv.setViewName("/web/news/personNewsInfo");
        PersonInfo personInfo = personInfoService.findById(id);
        mv.addObject(personInfo);
        //设为已读
        personInfo.setIsRead(true);
        personInfoService.update(personInfo);
        return mv;
    }
    //删除个人消息
    @RequestMapping("/personNews/del")
    public void personNewsDel(Integer id,HttpServletResponse response) throws IOException {
        PersonInfo personInfo = new PersonInfo();
        personInfo.setId(id);
        personInfo.setEnable(false);
        personInfoService.update(personInfo);
        PrintWriter out = response.getWriter();
        out.print(1);
    }
    @RequestMapping("/personNewsData")
    public ModelAndView personNewsData(HttpSession session){
        Person person = (Person)session.getAttribute("person");
        ModelAndView mv = new ModelAndView();
        mv.setViewName("web/news/personNewsData");
        PageInfo<PersonInfo> pageInfo = personInfoService.findAll(person);
        mv.addObject(pageInfo);
        mv.addObject(pageInfo.getList());
        return mv;
    }

    @RequestMapping("/personNews")
    public ModelAndView personNews(HttpSession session){
        Person person = (Person)session.getAttribute("person");
        ModelAndView mv = new ModelAndView();
        mv.setViewName("web/news/personNews");
        PageInfo<PersonInfo> pageInfo = personInfoService.findAll(person);
        mv.addObject(pageInfo);
        mv.addObject(pageInfo.getList());
        return mv;
    }


}
